package com.viewplug.view.swap;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Scroller;

/**
 * Created by mac_wk on 14-12-23.
 */
public class SwapListViewContent extends LinearLayout {
    private boolean first_flag;
    private int status;
    private Scroller mScroller;
    private View child;

    public SwapListViewContent(Context context) {
        this(context,null);
    }

    public SwapListViewContent(Context context, AttributeSet attrs) {
        super(context, attrs);

    }
    public SwapListViewContent(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public SwapListViewContent(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
    private void initViews() {
        mScroller = new Scroller(getContext());
        int count = getChildCount();
        if (count!=2)
            throw new IllegalStateException("参数不正确");
        child = getChildAt(1);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    public void hidden() {
        int total = getTotal_count();
        setStatus(SwapListView.HIDDEN);
        mScroller.startScroll(getRealWidth(), 0, ( -getRealWidth() ), 0, 400);
        total_count = 0;
        invalidate();
    }
    public void scroller(boolean b) {
        int total = getTotal_count();
        int real_width = getRealWidth();
        int final_width = 0;
        if (total >= (real_width / 2)) {
            final_width = real_width - total;
            setStatus(SwapListView.SHOW);
        } else {
            //如果是b那么就是因为速度快而过去
            if (b) {
                setStatus(SwapListView.SHOW);
                final_width = real_width - total;
            } else {
                final_width = -total;
            }

        }
        Log.d("total:",total+"");
        mScroller.startScroll(total, 0, final_width, 0, 400);
        total_count = 0;
        invalidate();
    }
    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getTotal_count() {
        return total_count;
    }

    private int total_count = 0;
    public void move(int x) {
        if (total_count<0)
            return;
        if (total_count>=child.getMeasuredWidth())
            return;
        total_count+=x;
        scrollTo(total_count, 0);
        invalidate();
        postInvalidate();
    }

    public int getRealWidth() {
        int width = child.getMeasuredWidth();
        return child.getMeasuredWidth();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        initViews();
    }
    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), 0);
            postInvalidate();
        }
        super.computeScroll();
    }
}
